# /lab_booking/routers/equipment.py

from typing import List
from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session

from lab_booking import crud, schemas
from lab_booking.database import get_db

router = APIRouter(
    prefix="/equipment", # 所有接口都在 /equipment 路径下
    tags=["设备信息 (Public Equipment Info)"]
)

@router.get("/categories", response_model=List[schemas.DeviceCategory])
def read_device_categories(db: Session = Depends(get_db)):
    """获取所有设备分类列表。"""
    return crud.get_device_categories(db=db)

@router.get("/all", response_model=List[schemas.Device])
def read_all_devices(db: Session = Depends(get_db)):
    """获取所有设备的完整列表，用于搜索。"""
    return crud.get_all_devices(db=db)
    
@router.get("/{category_id}/devices", response_model=List[schemas.Device])
def read_devices_by_category(category_id: int, db: Session = Depends(get_db)):
    """根据分类ID获取设备列表。"""
    devices = crud.get_devices_by_category(db, category_id=category_id)
    if not devices:
        return []
    return devices

@router.get("/{device_id}/details", response_model=schemas.Device)
def read_device_details(device_id: int, db: Session = Depends(get_db)):
    """根据 ID 获取单个设备的详细信息。"""
    db_device = crud.get_device_by_id(db, device_id=device_id)
    if db_device is None:
        raise HTTPException(status_code=404, detail="Device not found")
    return db_device

@router.get("/devices/{device_id}/", response_model=schemas.Device)
def read_device_details(device_id: int, db: Session = Depends(get_db)):
    """
    根据 ID 获取单个设备的详细信息。
    """
    db_device = crud.get_device_by_id(db, device_id=device_id)
    if db_device is None:
        raise HTTPException(status_code=404, detail="Device not found")
    return db_device
